package nl.infodation.emr.pricingservice.ui;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;
import nl.infodation.emr.pricingservice.controller.ControllerFactory;
import nl.infodation.emr.pricingservice.controller.IController;
import nl.infodation.emr.pricingservice.data.UserSearchItem;
import nl.infodation.emr.pricingservice.entity.Role;
import nl.infodation.emr.pricingservice.entity.SystemValue;
import nl.infodation.emr.pricingservice.entity.SystemValue.TableName;
import nl.infodation.emr.pricingservice.entity.User;
import nl.infodation.emr.pricingservice.ui.component.IView;
import nl.infodation.emr.pricingservice.ui.component.PagingComponent;
import nl.infodation.emr.pricingservice.ui.component.PagingModel;
import nl.infodation.emr.pricingservice.ui.page.SessionData;
import nl.infodation.emr.utility.Constants;
import nl.infodation.emr.utility.DateTimeUtil;
import nl.infodation.emr.utility.EncryptionUtil;
import nl.infodation.emr.utility.StringUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.vaadin.annotations.AutoGenerated;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.util.BeanItem;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.event.ItemClickEvent.ItemClickListener;
import com.vaadin.terminal.UserError;
import com.vaadin.ui.AbstractField;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.CustomLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.NativeButton;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Window.Notification;

@SuppressWarnings({ "unchecked", "rawtypes" })
public class UsersManagementView extends CustomComponent implements IView,
		ClickListener, ItemClickListener {

	private static final String CURRENT_PAGE = "currentPage";

	private CustomLayout userLayout;

	private Label lblCurrentPageUserList;
	private PagingComponent pagingComponentUserList;
	private PagingModel modelUserList = new PagingModel(0);

	private Label lblCurrentPageCountryList;
	private PagingComponent pagingComponentCountryList;
	private PagingModel modelCountryList = new PagingModel(0);

	private Button btnUpdate;

	private Button btnCancel;

	private Button btnDelete;

	private Button btnEdit;

	private Label lblBusinessLine;

	private Label lblBusinessHeader;

	private Label lblEnvironmentName;

	private UsersList usersList;

	private ComboBox cbbBusiness;

	private TextField tfLastName;

	private TextField tfFirstName;

	private PasswordField tfPassword;

	private TextField tfUsername;

	private Button btnSubmit;

	private Label lblLastName;

	private Label lblFirstName;

	private Label lblBusiness;

	private Label lblPassword;

	private Label lblUsername;

	private UserCountryCheckerList countryCheckerList;

	private Button btnAddNew;

	/**
	 * The constructor should first build the main layout, set the composition
	 * root and then do any custom initialization.
	 * 
	 * The constructor will not be automatically regenerated by the visual
	 * editor.
	 */
	List<BeanItem> countriesList;

	public UsersManagementView() {
		initLayout();
	}

	private static final long serialVersionUID = 1L;
	private static final Logger LOG = LoggerFactory
			.getLogger(UsersManagementView.class);
	private static User curUser;
	private boolean isUpdate = Boolean.FALSE;

	public static User getCurrentUser() {
		return curUser;
	}

	static public void setCurrentUser(User user) {
		curUser = user;
	}

	@Override
	public void initLayout() {
		userLayout = new CustomLayout("UserContent");
		buildMainLayout();
		setCaptionForMultiLanguage();
		addListener();
		setOptionForBusLine();
		initialModelPagingUserList();
		setCompositionRoot(userLayout);
	}

	private void setTotalNumberOfPagesForReloadFirst() {
		long totalItems = 0;
		if (SessionData.isSuperAdmin()) {
			UserSearchItem searchItem = new UserSearchItem();
			searchItem.setActive(Boolean.TRUE);
			totalItems = ControllerFactory.getUserController().getTotalNumberItemByCriteria(searchItem);
		} else if (SessionData.isNormalAdmin()) {
			UserSearchItem searchItem = new UserSearchItem();
			searchItem.setRoleName(SessionData.getRoleName());
			searchItem.setActive(Boolean.TRUE);
			totalItems = ControllerFactory.getUserController()
					.getTotalNumberItemByCriteria(searchItem);
		}

		final int totalPages = (int) Math.ceil(totalItems
				/ (float) usersList.getPageSize());
		modelUserList.setTotalNumberOfPages(totalPages);
	}

	private void initialModelPagingUserList() {
		setTotalNumberOfPagesForReloadFirst();
		modelUserList.addPropertyChangeListener(CURRENT_PAGE,
				new PropertyChangeListener() {
					public void propertyChange(final PropertyChangeEvent evt) {
						updatePageNumberUserList();
						usersList.loadPage(modelUserList.getCurrentPage());
						usersList.select(usersList.firstItemId());
					}
				});
		pagingComponentUserList.setModel(modelUserList);
		updatePageNumberUserList();
	}

	private void updatePageNumberUserList() {
		lblCurrentPageUserList.setValue(SessionData.getI18N().getMessage(
				Constants.CURRENT_PAGE_VALUE)
				+ modelUserList.getCurrentPage());
	}

	private void setTotalNumberOfPagesForReloadSecond() {
		List<BeanItem> items = new ArrayList<BeanItem>();
		if (SessionData.isSuperAdmin() || SessionData.isNormalAdmin()) {
			SystemValue systemValue = new SystemValue();
			systemValue.setSystemName(TableName.COUNTRY);

			items.addAll(ControllerFactory.getSystemValueController()
					.getPaginationByCriteria(systemValue, 1, Integer.MAX_VALUE));

		} else {
			for (SystemValue country : SessionData.getCountriesToSee()) {
				BeanItem item = new BeanItem(country);
				items.add(item);
			}
		}

		final long totalItems = items.size();
		final int totalPages = (int) Math.ceil(totalItems
				/ (float) countryCheckerList.getPageSize());
		modelCountryList.setTotalNumberOfPages(totalPages);
	}

	private void initialModelPagingCountryList() {
		setTotalNumberOfPagesForReloadSecond();

		modelCountryList.addPropertyChangeListener(CURRENT_PAGE,
				new PropertyChangeListener() {
					public void propertyChange(final PropertyChangeEvent evt) {
						updatePageNumberSecond();
						countryCheckerList.loadPage(modelCountryList
								.getCurrentPage());
					}
				});
		pagingComponentCountryList.setModel(modelCountryList);
		countryCheckerList.reload();
		updatePageNumberSecond();
	}

	private void updatePageNumberSecond() {
		lblCurrentPageCountryList.setValue(SessionData.getI18N().getMessage(
				Constants.CURRENT_PAGE_VALUE)
				+ modelUserList.getCurrentPage());
	}

	private void setOptionForBusLine() {
		cbbBusiness.setNullSelectionAllowed(Boolean.FALSE);
		cbbBusiness.removeAllItems();

		if (SessionData.isSuperAdmin()) {
			List<BeanItem> beans = ControllerFactory.getRoleController()
					.getPaginationList(1, Integer.MAX_VALUE);
			if (beans != null) {
				for (BeanItem item : beans) {
					Role role = (Role) item.getBean();
					cbbBusiness.addItem(role.getRoleId());
					cbbBusiness.setItemCaption(role.getRoleId(),
							role.getRoleName());
				}
			}
		} else {
			String roleName = SessionData.getRoleName();
			Role searchItem = new Role();
			searchItem.setRoleName(roleName);
			List<BeanItem> beans = ControllerFactory.getRoleController()
					.getPaginationByCriteria(searchItem, 1, Integer.MAX_VALUE);
			if (beans != null) {
				Role role = (Role) beans.get(0).getBean();
				cbbBusiness.addItem(role.getRoleId());
				cbbBusiness
						.setItemCaption(role.getRoleId(), role.getRoleName());
			}
		}

		if (StringUtil.isEmptyOrBlank((String) cbbBusiness.getValue())) {
			// choose first item
			cbbBusiness.setValue(cbbBusiness.getItemIds().iterator().next());
		}
	}

	private void removeError() {
		tfUsername.setComponentError(null);
		tfPassword.setComponentError(null);
	}

	private void setComponentError(final AbstractField component,
			final String msgError) {
		component.focus();
		component.setComponentError(new UserError(msgError));
	}

	private boolean validateUserName(String nameToValidate) {
		User seachItem = new User();
		seachItem.setUsername(nameToValidate);

		if (!isUpdate) {
			if (ControllerFactory.getUserController()
					.findByEntity(seachItem).size() > 0) {
				return Boolean.FALSE;
			}
		}
		return Boolean.TRUE;
	}

	public boolean validate() {
		removeError();

		if (StringUtil.isEmptyOrBlank((String) tfUsername.getValue())) {
			setComponentError(
					tfUsername,
					SessionData.getI18N().getMessage(
							Constants.USERNAME_REQUIRED_MESSAGE));
			return Boolean.FALSE;

		} else if (StringUtil.isEmptyOrBlank((String) tfPassword.getValue())) {
			setComponentError(
					tfPassword,
					SessionData.getI18N().getMessage(
							Constants.PASSWORD_REQUIRED_MESSAGE));
			return Boolean.FALSE;
		} else if (!validateCountries()) {
			getWindow().showNotification("Please choose least 1 country!",
					Notification.TYPE_WARNING_MESSAGE);
			return Boolean.FALSE;
		} else if (!validateUserName((String) tfUsername.getValue())) {
			setComponentError(
					tfUsername,
					SessionData.getI18N().getMessage(
							Constants.USERNAME_REQUIRED_MESSAGE));
			return Boolean.FALSE;
		}

		removeError();

		return Boolean.TRUE;
	}

	public void setBinding(Property envProperty) {
		lblEnvironmentName.setPropertyDataSource(envProperty);
	}

	private void addListener() {
		usersList.addListener((ItemClickListener) this);
		btnUpdate.addListener((ClickListener) this);
		btnCancel.addListener((ClickListener) this);
		btnSubmit.addListener((ClickListener) this);
		btnEdit.addListener((ClickListener) this);
		btnDelete.addListener((ClickListener) this);
		btnAddNew.addListener((ClickListener) this);

		lblEnvironmentName.setImmediate(Boolean.TRUE);
		lblEnvironmentName.addListener(new ValueChangeListener() {
			private static final long serialVersionUID = 1L;

			@Override
			public void valueChange(ValueChangeEvent event) {
				Property p = event.getProperty();
				if (p.getValue().equals(
						SessionData.getI18N().getMessage(
								Constants.UAT_ENVIROMENT_MESSAGE))) {
					lblEnvironmentName.setStyleName("greenHeaderLabel");

				} else if (p.getValue().equals(
						SessionData.getI18N().getMessage(
								Constants.PRE_PROD_ENVIROMENT_MESSAGE))) {
					lblEnvironmentName.setStyleName("orangeHeaderLabel");

				} else if (p.getValue().equals(
						SessionData.getI18N().getMessage(
								Constants.PROD_ENVIROMENT_MESSAGE))) {
					lblEnvironmentName.setStyleName("redHeaderLabel");
				}
			}
		});

		cbbBusiness.setImmediate(Boolean.TRUE);
		cbbBusiness.addListener(new ValueChangeListener() {
			private static final long serialVersionUID = 1L;

			@Override
			public void valueChange(ValueChangeEvent event) {
				Property p = event.getProperty();
				lblBusinessLine.setValue(p.getValue());
			}
		});
	}

	private void setCaptionForMultiLanguage() {
		lblBusinessHeader.setValue(SessionData.getI18N().getMessage(
				Constants.BUSINESS_LABEL));
		tfUsername.setCaption(SessionData.getI18N().getMessage(
				Constants.USERNAME_LABEL));
		tfPassword.setCaption(SessionData.getI18N().getMessage(
				Constants.PASSWORD_LABEL));
		cbbBusiness.setCaption(SessionData.getI18N().getMessage(
				Constants.BUSINESS_LABEL));
		tfFirstName.setCaption(SessionData.getI18N().getMessage(
				Constants.FIRSTNAME_LABEL));
		tfLastName.setCaption(SessionData.getI18N().getMessage(
				Constants.LASTNAME_LABEL));

		btnEdit.setCaption(SessionData.getI18N().getMessage(
				Constants.EDIT_BUTTON));
		btnDelete.setCaption(SessionData.getI18N().getMessage(
				Constants.DELETE_BUTTON));
		btnSubmit.setCaption(SessionData.getI18N().getMessage(
				Constants.SUBMIT_BUTTON));

		userLayout.addComponent(
				new Label(SessionData.getI18N().getMessage(
						Constants.USERS_BUTON)), "lbUser");
	}

	private void resetField() {
		tfFirstName.setValue("");
		tfLastName.setValue("");
		tfPassword.setValue("");
		tfUsername.setValue("");
		cbbBusiness.setValue(cbbBusiness.getItemIds().iterator().next());
		// choose first item
		removeError();
	}

	@Override
	public void reload() {
		setTotalNumberOfPagesForReloadFirst();
		usersList.reload();
		userLayout.removeComponent("lcUserList");
		userLayout.addComponent(usersList, "lcUserList");
		clearUserInfo();
		

	}

	private void clearUserInfo() {
		resetField();
		isUpdate = Boolean.FALSE;
		List<SystemValue> tmp = curUser.getCountries();
		curUser.setCountries(null);
		UserCountryCheckerList.getActiveList().clear();
		initCountriesChooser(countriesList);
		curUser.setCountries(tmp);
		tfUsername.setEnabled(Boolean.TRUE);
		userLayout.removeComponent(btnUpdate);
		userLayout.addComponent(btnSubmit, "lcBtnAddNew");
	}

	private void initialUserInfo() {
		isUpdate = Boolean.TRUE;
		UserCountryCheckerList.getActiveList().clear();
		userLayout.removeComponent(btnSubmit);
		userLayout.addComponent(btnUpdate, "lcBtnAddNew");
		tfUsername.setEnabled(Boolean.FALSE);
		setOptionForBusLine();

		tfUsername.setValue(curUser.getUsername());
		tfPassword.setValue("");
		tfFirstName.setValue(curUser.getFirstName());
		tfLastName.setValue(curUser.getLastName());
		if (curUser.isSuperAdmin()) {
			cbbBusiness.setValue(null);
			cbbBusiness.setEnabled(false);
		} else {
			cbbBusiness.setEnabled(true);
			userLayout.addComponent(cbbBusiness, "lcCobBussiness");
		}

		if (curUser.isNormalAdmin() || curUser.isSuperAdmin()) {
			userLayout.removeComponent("lcCountriesChooser");
			userLayout.removeComponent("lcPageEdit");
			userLayout.removeComponent("lcCurrentPageEdit");
		} else {
			SystemValue systemValue = new SystemValue();
			systemValue.setSystemName(TableName.COUNTRY);
			initCountriesChooser(ControllerFactory.getSystemValueController()
					.getPaginationByCriteria(systemValue, 1, Integer.MAX_VALUE));
		}
		Role role = curUser.getRole();
		if (role != null) {
			cbbBusiness.setValue(role.getRoleId());
		}
		if (StringUtil.isEmptyOrBlank(String.valueOf(cbbBusiness.getValue()))) {
			cbbBusiness.setValue(cbbBusiness.getItemIds().iterator().next()); // choose

		}
	}

	private void initCountriesChooser(List<BeanItem> countriesSources) {
		userLayout.removeComponent(countryCheckerList);
		userLayout.removeComponent(pagingComponentCountryList);
		userLayout.removeComponent(lblCurrentPageCountryList);

		// countryCheckerList_1
		countryCheckerList = new UserCountryCheckerList();
		countryCheckerList.setImmediate(Boolean.TRUE);
		countryCheckerList.setWidth("335px");
		countryCheckerList.setHeight("200px");

		countryCheckerList.setStyleName("country");
		countryCheckerList.setDataSource(countriesSources);

		initialModelPagingCountryList();

		userLayout.addComponent(countryCheckerList, "lcCountriesChooser");
		userLayout.addComponent(pagingComponentCountryList, "lcPageEdit");
		userLayout.addComponent(lblCurrentPageCountryList, "lcCurrentPageEdit");
	}

	private void editUser() {
		initialUserInfo();
	}

	@Override
	public void buttonClick(ClickEvent event) {
		final Button source = event.getButton();
		if (source.equals(btnSubmit)) {
			LOG.info(DateTimeUtil.getNow() + "Click submit button");
			if (!validate()) {
				return;
			}
			boolean saved = Boolean.FALSE;
			IController userController = ControllerFactory.getUserController();

			User user = new User();
			user.setUsername((String) this.tfUsername.getValue());

			String pwd = (String) this.tfPassword.getValue();
			String encrypted = EncryptionUtil.encryption(pwd);
			user.setPassword(encrypted);

			// Set Role value
			Role role = (Role) ControllerFactory.getRoleController()
					.getObjectById((Integer) cbbBusiness.getValue());
			user.setRole(role);

			user.setFirstName((String) this.tfFirstName.getValue());
			user.setLastName((String) this.tfLastName.getValue());

			List<SystemValue> countries = new ArrayList<SystemValue>();

			for (int id : UserCountryCheckerList.getActiveList()) {
				SystemValue country = (SystemValue) ControllerFactory
						.getSystemValueController().getObjectById(id);
				countries.add(country);
			}

			user.setActive(Boolean.TRUE);
			saved = userController.saveObject(user);

			if (saved) {
				user.setCountries(countries);
				saved = userController.saveObject(user);
			}

			if (saved) {
				this.getParent().getWindow()
						.executeJavaScript("hidePopup($('#popup_new'));");
				reload();
				modelUserList.setCurrentPage(1);
				getWindow().showNotification(
						SessionData.getI18N().getMessage(
								Constants.SUCCESSFULLY_MESSAGE),
						Notification.TYPE_WARNING_MESSAGE);

			} else {
				getWindow().showNotification(
						SessionData.getI18N().getMessage(
								Constants.FAILED_MESSAGE),
						Notification.TYPE_WARNING_MESSAGE);
			}

		} else if (source.equals(btnEdit)) {
			LOG.info(DateTimeUtil.getNow() + "Click edit button");
			editUser();
		} else if (source.equals(btnAddNew)) {
			cbbBusiness.setEnabled(Boolean.TRUE);
		} else if (source.equals(btnDelete)) {
			LOG.info(DateTimeUtil.getNow() + "Click delete button");

			if (curUser.isSuperAdmin() || curUser.isNormalAdmin()) {
				getWindow().showNotification(
						SessionData.getI18N().getMessage(
								Constants.DELETE_USER_SUPPER_MESSAGE),
						Notification.TYPE_WARNING_MESSAGE);
			} else {
				User user = new User();
				user.setUsername(curUser.getUsername());
				user.setPassword(curUser.getPassword());
				user.setNormalAdmin(Boolean.FALSE);
				user.setSuperAdmin(Boolean.FALSE);
				user.setRole(curUser.getRole());
				user.setFirstName(curUser.getFirstName());
				user.setFirstName(curUser.getLastName());
				user.setActive(false);

				ControllerFactory.getUserController().saveObject(user);
				getWindow().showNotification(
						SessionData.getI18N().getMessage(
								Constants.DELETE_USER_NORMAL_MESSAGE),
						Notification.TYPE_WARNING_MESSAGE);
				usersList.reload();
				usersList.selectFirstItem();
				modelUserList.setCurrentPage(1);
			}

		} else if (source == btnUpdate) {
			if (validate() == false) {
				return;
			}
			boolean saved = Boolean.FALSE;
			IController userController = ControllerFactory.getUserController();
			User user = new User();
			user.setUsername((String) this.tfUsername.getValue());

			String pwd = (String) this.tfPassword.getValue();
			String encrypted = EncryptionUtil.encryption(pwd);
			user.setPassword(encrypted);

			// Set Role value
			Role role = (Role) ControllerFactory.getRoleController()
					.getObjectById((Integer) cbbBusiness.getValue());
			user.setRole(role);

			user.setFirstName((String) this.tfFirstName.getValue());
			user.setLastName((String) this.tfLastName.getValue());

			if (null == user.getCountries()) {
				user.setCountries(new ArrayList<SystemValue>());
			}
			user.getCountries().clear();
			for (int id : UserCountryCheckerList.getActiveList()) {
				SystemValue country = (SystemValue) ControllerFactory
						.getSystemValueController().getObjectById(id);
				user.getCountries().add(country);
			}
			user.setNormalAdmin(getCurrentUser().isNormalAdmin());
			user.setSuperAdmin(getCurrentUser().isSuperAdmin());
			user.setActive(Boolean.TRUE);
			saved = userController.saveObject(user);

			if (saved) {
				isUpdate = Boolean.FALSE;
				// Close the popup
				this.getParent().getWindow()
						.executeJavaScript("hidePopup($('#popup_new'));");
				reload();
				getApplication().getMainWindow().removeWindow(this.getWindow());

			} else {
				reload();
				getWindow().showNotification(
						SessionData.getI18N().getMessage(
								Constants.FAILED_MESSAGE),
						Notification.TYPE_WARNING_MESSAGE);
			}

		} else if (source == btnCancel) {
			isUpdate = Boolean.FALSE;
			clearUserInfo();
			this.getParent().getWindow()
					.executeJavaScript("hidePopup($('#popup_new'));");
		}
	}

	private boolean validateCountries() {
		if (isUpdate) {
			if (curUser.isNormalAdmin() || curUser.isSuperAdmin())
				return Boolean.TRUE;
		}
		if (UserCountryCheckerList.getActiveList().size() > 0)
			return Boolean.TRUE;
		return Boolean.FALSE;
	}

	@Override
	public void itemClick(ItemClickEvent event) {
		BeanItem bean = (BeanItem) event.getItem();
		setCurrentUser((User) bean.getBean());
		if (event.isDoubleClick()) {
			editUser();
			// call javascript open form update'
			this.getParent().getWindow()
					.executeJavaScript("showPopup($('#popup_new'));");
		}

	}

	@AutoGenerated
	private void buildMainLayout() {

		// pagingComponent_1
		pagingComponentUserList = new PagingComponent();
		pagingComponentUserList.setImmediate(Boolean.FALSE);
		pagingComponentUserList.setStyleName("inline");
		userLayout.addComponent(pagingComponentUserList, "lcPage");

		// lblCurrentPage
		lblCurrentPageUserList = new Label();
		lblCurrentPageUserList.setImmediate(Boolean.FALSE);
		lblCurrentPageUserList.setStyleName("inline");
		lblCurrentPageUserList.setValue("Current Page: ?");
		userLayout.addComponent(lblCurrentPageUserList, "lcCurrentPage");

		// pagingComponent_2
		pagingComponentCountryList = new PagingComponent();
		pagingComponentCountryList.setImmediate(Boolean.FALSE);
		pagingComponentCountryList.setStyleName("pagingusercountry");
		userLayout.addComponent(pagingComponentCountryList, "lcPageEdit");

		// lblCurrentPage
		lblCurrentPageCountryList = new Label();
		lblCurrentPageCountryList.setImmediate(Boolean.FALSE);
		lblCurrentPageCountryList.setStyleName("inline");
		lblCurrentPageCountryList.setValue("Current Page: ?");
		userLayout.addComponent(lblCurrentPageCountryList, "lcCurrentPageEdit");

		// usersList
		usersList = new UsersList(this);
		usersList.setImmediate(Boolean.FALSE);
		usersList.setColumnCollapsingAllowed(Boolean.FALSE);
		usersList.setSizeFull();
		usersList.setWidth("736px");
		usersList.setHeight("620px");
		userLayout.addComponent(usersList, "lcUserList");

		// lblEnvironmentName
		lblEnvironmentName = new Label();
		lblEnvironmentName.setStyleName("headerLabel");
		lblEnvironmentName.setImmediate(Boolean.FALSE);
		lblEnvironmentName.setWidth("-1px");
		lblEnvironmentName.setHeight("-1px");
		lblEnvironmentName.setValue("UAT Environment");

		// lblBusinessHeader
		lblBusinessHeader = new Label();
		// lblBusinessHeader.setStyleName("captionLabel");
		lblBusinessHeader.setImmediate(Boolean.FALSE);
		lblBusinessHeader.setValue("Business:");

		// lblBusinessLine
		lblBusinessLine = new Label();
		lblBusinessLine.setImmediate(Boolean.FALSE);
		lblBusinessLine.setValue("PROVEA");

		buildAbsoluteLayoutSecond();
		buildVerticalLayoutFirst();

		SystemValue systemValue = new SystemValue();
		systemValue.setSystemName(TableName.COUNTRY);
		countriesList = ControllerFactory.getSystemValueController()
				.getPaginationByCriteria(systemValue, 1, Integer.MAX_VALUE);

	}

	@AutoGenerated
	private void buildAbsoluteLayoutSecond() {

		// lblUsername
		lblUsername = new Label();
		lblUsername.setImmediate(Boolean.FALSE);
		lblUsername.setValue("Username");

		// lblPassword
		lblPassword = new Label();
		lblPassword.setImmediate(Boolean.FALSE);
		lblPassword.setValue("Password");

		// lblBusiness
		lblBusiness = new Label();
		lblBusiness.setImmediate(Boolean.FALSE);
		lblBusiness.setValue("Business");

		// lblFirstName
		lblFirstName = new Label();
		lblFirstName.setImmediate(Boolean.FALSE);
		lblFirstName.setValue("First Name");

		// lblLastName
		lblLastName = new Label();
		lblLastName.setImmediate(Boolean.FALSE);
		lblLastName.setValue("LastName");

		// btnSubmit
		btnSubmit = new NativeButton();
		btnSubmit.setCaption("Add");
		btnSubmit.setStyleName("button_style_popup");
		btnSubmit.setImmediate(Boolean.TRUE);
		userLayout.addComponent(btnSubmit, "lcBtnAddNew");

		// btnUpdate
		btnUpdate = new NativeButton();
		btnUpdate.setCaption(SessionData.getI18N().getMessage(
				Constants.UPDATE_BUTTON));
		btnUpdate.setStyleName("button_style_popup");
		btnUpdate.setImmediate(Boolean.TRUE);

		// btnCancel
		btnCancel = new NativeButton();
		btnCancel.setCaption(SessionData.getI18N().getMessage(
				Constants.CANCEL_BUTTON));
		btnCancel.setStyleName("small_button");
		btnCancel.setImmediate(Boolean.TRUE);
		userLayout.addComponent(btnCancel, "lcBtnCancel");

		// tfUsername
		tfUsername = new TextField();
		tfUsername.setCaption("Username");
		tfUsername.setStyleName("inline_field_popup");
		tfUsername.setImmediate(Boolean.FALSE);
		userLayout.addComponent(tfUsername, "lcTfUserName");

		// tfPassword
		tfPassword = new PasswordField();
		tfPassword.setCaption("Password");
		tfPassword.setStyleName("inline_field_popup");
		tfPassword.setImmediate(Boolean.FALSE);
		userLayout.addComponent(tfPassword, "lcTfPassword");

		// tfFirstName
		tfFirstName = new TextField();
		tfFirstName.setCaption("First Name");
		tfFirstName.setStyleName("inline_field_popup");
		tfFirstName.setImmediate(Boolean.FALSE);
		userLayout.addComponent(tfFirstName, "lcTfFirstName");

		// tfLastName
		tfLastName = new TextField();
		tfLastName.setImmediate(Boolean.FALSE);
		tfLastName.setCaption("Last Name");
		tfLastName.setStyleName("inline_field_popup");
		userLayout.addComponent(tfLastName, "lcTfLastName");

		// cbbBusiness
		cbbBusiness = new ComboBox();
		cbbBusiness.setCaption("Business");
		cbbBusiness.setStyleName("inline_field_popup");
		cbbBusiness.setImmediate(Boolean.FALSE);
		userLayout.addComponent(cbbBusiness, "lcCobBussiness");
	}

	@AutoGenerated
	private void buildVerticalLayoutFirst() {

		// btnAdd
		btnAddNew = new NativeButton();
		btnAddNew.setImmediate(true);
		btnAddNew.setStyleName("right_button");
		btnAddNew.addStyleName("usereadd");
		userLayout.addComponent(btnAddNew, "btnAddNew");

		// btnEdit
		btnEdit = new NativeButton();
		btnEdit.setCaption("Edit");
		btnEdit.setImmediate(Boolean.FALSE);
		btnEdit.setStyleName("right_button");
		btnEdit.addStyleName("useredit");
		userLayout.addComponent(btnEdit, "lcBtnEditUser");
		userLayout.addComponent(
				new Label(SessionData.getI18N().getMessage(
						Constants.ADD_NEW_USER_PANEL)), "lcTipAddNew");
		userLayout.addComponent(
				new Label(SessionData.getI18N().getMessage(
						Constants.EDIT_BUTTON)), "lcTipEdit");

		// btnDelete
		btnDelete = new NativeButton();
		btnDelete.setCaption("Deactive");
		btnDelete.setImmediate(Boolean.TRUE);
		btnDelete.setStyleName("right_button");
		btnDelete.addStyleName("remove");
		userLayout.addComponent(btnDelete, "lcBtnDeleteUser");
		userLayout.addComponent(
				new Label(SessionData.getI18N().getMessage(
						Constants.DELETE_BUTTON)), "lcTipDelete");
	}
}
